﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client
{
    public partial class FrmSelectSongs : UIForm
    {
        int currentPage = 1;
        int totalPage = 0;
        Song[] songs = new Song[20];

       
        public FrmSelectSongs()
        {
            InitializeComponent();

            // 设置Label 透明
            this.label1.Parent = this.pictureBox1;
            this.label1.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label2.Parent = this.pictureBox1;
            this.label2.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label3.Parent = this.pictureBox1;
            this.label3.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label4.Parent = this.pictureBox1;
            this.label4.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label5.Parent = this.pictureBox1;
            this.label5.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label6.Parent = this.pictureBox1;
            this.label6.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label7.Parent = this.pictureBox1;
            this.label7.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label8.Parent = this.pictureBox1;
            this.label8.BackColor = Color.Transparent;

            // 设置Label 透明
            this.label9.Parent = this.pictureBox1;
            this.label9.BackColor = Color.Transparent;

            // 设置panel 透明
            this.panel1.Parent = this.pictureBox1;
            this.panel1.BackColor = Color.Transparent;

            this.label10.Parent = this.pictureBox1;
            this.label10.BackColor = Color.Transparent;

            this.label11.Parent = this.pictureBox1;
            this.label11.BackColor = Color.Transparent;

            this.label12.Parent = this.pictureBox1;
            this.label12.BackColor = Color.Transparent;

            this.label13.Parent = this.pictureBox1;
            this.label13.BackColor = Color.Transparent;

            this.label14.Parent = this.pictureBox1;
            this.label14.BackColor = Color.Transparent;

            this.label15.Parent = this.pictureBox1;
            this.label15.BackColor = Color.Transparent;

            this.label16.Parent = this.pictureBox1;
            this.label16.BackColor = Color.Transparent;

            this.label17.Parent = this.pictureBox1;
            this.label17.BackColor = Color.Transparent;

            this.label18.Parent = this.pictureBox1;
            this.label18.BackColor = Color.Transparent;

            this.label19.Parent = this.pictureBox1;
            this.label19.BackColor = Color.Transparent;

            this.label20.Parent = this.pictureBox1;
            this.label20.BackColor = Color.Transparent;

            this.label21.Parent = this.pictureBox1;
            this.label21.BackColor = Color.Transparent;

            this.label22.Parent = this.pictureBox1;
            this.label22.BackColor = Color.Transparent;

            this.label23.Parent = this.pictureBox1;
            this.label23.BackColor = Color.Transparent;

            this.label24.Parent = this.pictureBox1;
            this.label24.BackColor = Color.Transparent;

            this.uiLabel2.Parent = this.pictureBox1;
            this.uiLabel2.BackColor = Color.Transparent;

            this.uiLabel3.Parent = this.pictureBox1;
            this.uiLabel3.BackColor = Color.Transparent;

            this.uiLabel4.Parent = this.pictureBox1;
            this.uiLabel4.BackColor = Color.Transparent;

            this.uiLabel5.Parent = this.pictureBox1;
            this.uiLabel5.BackColor = Color.Transparent;

            this.uiLabel6.Parent = this.pictureBox1;
            this.uiLabel6.BackColor = Color.Transparent;

            this.uiLabel7.Parent = this.pictureBox1;
            this.uiLabel7.BackColor = Color.Transparent;

            this.uiLabel8.Parent = this.pictureBox1;
            this.uiLabel8.BackColor = Color.Transparent;

            this.uiScrollingText1.Parent = this.pictureBox1;
            this.uiScrollingText1.FillColor = Color.Transparent;

            // 设置panel 透明
            this.label25.Parent = this.uiPanel1;
            this.label25.BackColor = Color.Transparent;

            //this.uiPanel1.Parent = this.pictureBox2;
            //this.uiPanel1.BackColor = Color.Transparent;

            // 设置panel 透明  此处会导致画面闪烁，暂时没有解决
            this.panel2.Parent = this.uiPanel1;
            this.panel2.BackColor = Color.Transparent;

            this.label26.Parent = this.pictureBox1;
            this.label26.BackColor = Color.Transparent;

            this.uiLabel10.Parent = this.pictureBox1;
            this.uiLabel10.BackColor = Color.Transparent;

            this.uiLabel11.Parent = this.pictureBox1;
            this.uiLabel11.BackColor = Color.Transparent;

        }

        private void label9_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmMain frmMain = new FrmMain();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试绑定点击事件");
        }



        private void label10_Click(object sender, EventArgs e)
        {
            int Num = 0;
            PictureBox[] pb;
            Num = 24;
            pb = new PictureBox[Num];
            for (int i = 0; i < 12; i++)
            {
                pb[(currentPage-1)*12 + i] = new System.Windows.Forms.PictureBox();
                pb[(currentPage - 1) * 12 + i].Width = 124;
                pb[(currentPage - 1) * 12 + i].Height = 200;
                pb[(currentPage - 1) * 12 + i].BorderStyle = BorderStyle.FixedSingle;
                if (i <= 5)
                {
                    pb[(currentPage - 1) * 12 + i].Location = new Point(i * 149 + 16, 13);
                }
                else {
                    pb[(currentPage - 1) * 12 + i].Location = new Point((i-6) * 149 + 16, 223);
                }
                pb[(currentPage - 1) * 12 + i].SizeMode = PictureBoxSizeMode.Zoom;
                if (i < 9 && currentPage == 1)
                {
                    pb[(currentPage - 1) * 12 + i].Image = Image.FromFile(@"D:\pic\pic_00" + (i+1 + (currentPage - 1) * 12) + ".jpg");
                }
                else
                {
                    pb[(currentPage - 1) * 12 + i].Image = Image.FromFile(@"D:\pic\pic_0" + (i + 1 + (currentPage - 1)*12) + ".jpg");
                }
                panel1.Controls.Add(pb[(currentPage - 1) * 12 + i]);
            }
            totalPage = Num / 12;
            this.label15.Text = "第"+currentPage+"页/共"+ Num/12+"页";
        }

        private void Singer_clear(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
            {
                panel1.Controls.RemoveAt(0);
            }
        }

        private void label17_Click(object sender, EventArgs e)
        {
            currentPage++;
            if (currentPage == totalPage)
            {
                this.label17.Enabled = false;
                this.label16.Enabled = true;
            }
            else
            {
                this.label17.Enabled = true;
            }
            Singer_clear(sender, e);
            label10_Click(sender,e);
            
        }

        private void label16_Click(object sender, EventArgs e)
        {
            currentPage--;
            if (currentPage != 1)
            {
                this.label16.Enabled = true;
            }
            else
            {
                this.label16.Enabled = false;
                this.label17.Enabled = true;
            }
            Singer_clear(sender, e);
            label10_Click(sender, e);
        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {
            //https://blog.csdn.net/Dream_sunny/article/details/79982579
            //https://blog.csdn.net/dqs78833488/article/details/52034674
            axWindowsMediaPlayer1.URL = @"D:\songs\年会舞蹈红昭愿.mp3";
            uiScrollingText1.Text = axWindowsMediaPlayer1.currentMedia.name;
            axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(axWindowsMediaPlayer1.URL));
            addSongToList(axWindowsMediaPlayer1.URL, uiScrollingText1.Text);
        }

        private void addSongToList(string songPath,string songName) {
            for (int i = 0; i < songs.Length; i++)
            {
                if (songs[i] == null) {
                    songs[i] = new Song();
                    songs[i].Songid = i + 1;
                    songs[i].Songname = songName;
                    songs[i].Songpath = songPath;
                    break;
                }
            }
        }

        private void uiLabel4_Click(object sender, EventArgs e)
        {
            uiTrackBar1.Visible = true;
            uiTrackBar1.Value = axWindowsMediaPlayer1.settings.volume;
        }

        private void FrmSelectSongs_Load(object sender, EventArgs e)
        {
            label10_Click(sender,e);
        }

        private void uiTrackBar1_ValueChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = uiTrackBar1.Value;
        }

        private void uiTrackBar1_MouseLeave(object sender, EventArgs e)
        {
            uiTrackBar1.Visible = false;
            UIMessageTip.ShowOk("当前音量"+ axWindowsMediaPlayer1.settings.volume);
        }

        //https://www.cnblogs.com/GGLoner/p/6700286.html 图片文件 属性 选择始终复制，那么引用时可以写相对路径
        private void uiLabel5_Click(object sender, EventArgs e)
        {
            if ("播放".Equals(label24.Text)) {
                uiLabel5.Image = Image.FromFile(@"Resources\暂停.png");
                label24.Text = "暂停";
                axWindowsMediaPlayer1.Ctlcontrols.pause();
            } else if("暂停".Equals(label24.Text))
            {
                uiLabel5.Image = Image.FromFile(@"Resources\播放.png");
                label24.Text = "播放";
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
        }

        bool isShow = false;
        private void uiLabel9_MouseClick(object sender, MouseEventArgs e)
        {
            int elements = this.panel2.Controls.Count;
            if (elements > 0)
            {
                for (int i = 0; i < elements; i++)
                {
                    this.panel2.Controls.RemoveAt(0);
                }
            }
            isShow = true;
            bool flag = false;
            for (int i = 0; i < songs.Length; i++)
            {
                if (songs[i] != null) {
                    UILabel lbl = new UILabel();
                    lbl.Parent = this.uiPanel1;
                    lbl.Width = 200;
                    lbl.Height = 50;
                    lbl.TextAlign = ContentAlignment.MiddleLeft;
                    lbl.BackColor = Color.Transparent;
                    lbl.ForeColor = Color.Black;
                    lbl.Location = new Point(20 , i * 50 + 35);
                    lbl.Text = songs[i].Songid + "       " + songs[i].Songname;
                    this.panel2.Controls.Add(lbl);
                    flag = true;
                }
            }
            if (!flag) {
                Label lbl = new Label();
                lbl.Width = 270;
                lbl.Height = 35;
                lbl.Location = new Point(10, 200);
                lbl.Text = "没有点播任何歌曲!";
                lbl.ForeColor = Color.Red;
                lbl.TextAlign = ContentAlignment.MiddleCenter;
                lbl.Click += new System.EventHandler(this.button1_Click);
                this.panel2.Controls.Add(lbl);
            }
        }
        int minPreXposition = 10;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isShow && uiPanel1.Location.X > 1190)
            {
                uiPanel1.Location = new Point(uiPanel1.Location.X - minPreXposition, uiPanel1.Location.Y);
            }
            else {
                
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (!isShow && uiPanel1.Location.X <= 1480)
            {
                uiPanel1.Location = new Point(uiPanel1.Location.X + minPreXposition, uiPanel1.Location.Y);
            }
            else
            {

            }
        }

        private void label25_Click(object sender, EventArgs e)
        {
            isShow = false;
        }

        private void uiLabel11_Click(object sender, EventArgs e)
        {
            //axWindowsMediaPlayer1.URL = @"D:\songs\黄征羽泉奔跑.mp3";
            //uiScrollingText1.Text = axWindowsMediaPlayer1.currentMedia.name;
            addSongToList(this.uiLabel11.TagString, "奔跑");
            axWindowsMediaPlayer1.currentPlaylist.appendItem(axWindowsMediaPlayer1.newMedia(@"D:\songs\黄征羽泉奔跑.mp3"));
            //MessageBox.Show(Convert.ToString(axWindowsMediaPlayer1.currentPlaylist.count));
        }

        private void uiLabel9_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel8_Click(object sender, EventArgs e)
        {

            string sourcePath =  axWindowsMediaPlayer1.currentMedia.sourceURL;

            if (songs != null)
            {
                for (int i = 0; i < songs.Length-1; i++)
                {
                    if (songs[i] != null && songs[i+1] != null  && sourcePath.Equals(songs[i].Songpath))
                    {
                        axWindowsMediaPlayer1.URL = songs[i+1].Songpath;
                        uiScrollingText1.Text = axWindowsMediaPlayer1.currentMedia.name;
                    }
                }
            }
           
        }
    }
}
